package org.jeecg.modules.scm.service.impl;

import cn.hutool.core.util.URLUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.modules.scm.entity.ScmInvoice;
import org.jeecg.modules.scm.mapper.ScmInvoiceMapper;
import org.jeecg.modules.scm.service.IScmInvoiceService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.jeecg.modules.scm.common.util.HttpRequest.PostRequestFile;
import static org.jeecg.modules.scm.common.util.StringConversion.changeForFuzzy;

/**
 * @Description: scm_invoice
 * @Author: jeecg-boot
 * @Date:   2021-11-03
 * @Version: V1.0
 */
@Service
public class ScmInvoiceServiceImpl extends ServiceImpl<ScmInvoiceMapper, ScmInvoice> implements IScmInvoiceService {

    @Value("${ocr.address}")
    String address;
    @Value("${ocr.port}")
    String port;
    @Value("${ocr.path}")
    String path;

    @Override
    public String uploadAccessory(String id,MultipartFile file) {
//        System.out.println("enter--------------------------------------------------------------------");
//        ScmInvoice scmInvoice = lambdaQuery().select(ScmInvoice::getId,ScmInvoice::getInvoiceAttachment).eq(ScmInvoice::getId, invoiceId).one();
//        System.out.println(scmInvoice);
//        if (scmInvoice == null) {
//            throw new JeecgBootException("发票不存在");
//        }
//        String urls = scmInvoice.getInvoiceAttachment();

//        String url = address+":"+port+path;
//        Map<String,Object> res = PostRequestFile(url,file);
//        System.out.println("res:"+res);
        ScmInvoice scmInvoice = baseMapper.selectById(id);
        String url = CommonUtils.upload( file, "/invoice", CommonConstant.UPLOAD_TYPE_MINIO);
        String path = URLUtil.getPath(url);
        if(url==null){
            throw new JeecgBootException("上传失败");
        }
        scmInvoice.setInvoiceAttachment(path);
        baseMapper.updateById(scmInvoice);
//        res.put("path",path);
        return path;
    }

    /**
     * 模糊查询
     * */

    @Override
    public List<ScmInvoice> fuzzyQuery(String type, String value, String fields) {
        String res = changeForFuzzy(value);
        System.out.println(type+","+fields);
        if(fields!= null&&!fields.isEmpty()){
            fields = type+","+fields;
        }else{
            fields = type;
        }
        QueryWrapper<ScmInvoice> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(fields).like(type, res);
        List<ScmInvoice> list = baseMapper.selectList(queryWrapper);
        return list;
    }

    /**
     * 批量查询
     * **/
    @Override
    public List<ScmInvoice> queryBatch(String ids) {
        List<String> allId = Arrays.asList(ids.split(","));
        List<ScmInvoice> list = new ArrayList<>();
        for(String id:allId){
            ScmInvoice scmInvoice = baseMapper.selectById(id);
            if (scmInvoice != null) {
                list.add(scmInvoice);
            } else {
                throw new JeecgBootException("发票信息不存在");
            }
        }
        return list;
    }

    @Override
    public List<ScmInvoice> queryByIds(List<Integer> ids) {
        // 没有子查询的情况用in也可以，用循环查询反而会占用带宽
        return lambdaQuery().in(ScmInvoice::getId, ids).list();
    }

    @Override
    public Map<Integer, String> queryInvoiceNos(List<Integer> ids) {
        List<ScmInvoice> list = lambdaQuery().select(ScmInvoice::getId, ScmInvoice::getInvoiceNo).in(ScmInvoice::getId, ids).list();

        return list.stream().collect(Collectors.toMap(ScmInvoice::getId, ScmInvoice::getInvoiceNo));
    }

    /**
     * OCR
     */
    @Override
    public Map<String, Object> OCR(MultipartFile file) {
        String url = address + ":" + port + path;
        Map<String, Object> res = null;
        try {
            res = PostRequestFile(url, file);
        } catch (Exception e) {
            throw new JeecgBootException("网络繁忙，稍后再试");
        }
        return res;
    }

    @Override
    public Boolean updateStateByNo(String invoiceNo) {
        // 状态为0的时候才给更新
        return lambdaUpdate().set(ScmInvoice::getState, 1).eq(ScmInvoice::getInvoiceNo, invoiceNo).eq(ScmInvoice::getState, 0).update();
    }
}
